package com.rbac.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rbac.modules.system.dto.LoginLogQuery;
import com.rbac.modules.system.entity.SysLoginLog;
import com.rbac.modules.system.mapper.SysLoginLogMapper;
import com.rbac.modules.system.service.SysLoginLogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;

@Service
public class SysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLog> implements SysLoginLogService {

    @Override
    public Page<SysLoginLog> page(LoginLogQuery query) {
        LambdaQueryWrapper<SysLoginLog> wrapper = new LambdaQueryWrapper<>();

        // 添加查询条件
        wrapper.like(StringUtils.hasText(query.getUsername()), SysLoginLog::getUsername, query.getUsername())
                .like(StringUtils.hasText(query.getIp()), SysLoginLog::getIp, query.getIp())
                .like(StringUtils.hasText(query.getBrowser()), SysLoginLog::getBrowser, query.getBrowser())
                .like(StringUtils.hasText(query.getOs()), SysLoginLog::getOs, query.getOs())
                .eq(query.getStatus() != null, SysLoginLog::getStatus, query.getStatus())
                .ge(query.getStartTime() != null, SysLoginLog::getLoginTime, query.getStartTime())
                .le(query.getEndTime() != null, SysLoginLog::getLoginTime, query.getEndTime());

        // 添加排序
        if (StringUtils.hasText(query.getOrderBy())) {
            String orderBy = query.getOrderBy();
            boolean isAsc = "asc".equalsIgnoreCase(query.getOrderType());

            switch (orderBy) {
                case "username" -> wrapper.orderBy(true, isAsc, SysLoginLog::getUsername);
                case "ip" -> wrapper.orderBy(true, isAsc, SysLoginLog::getIp);
                case "browser" -> wrapper.orderBy(true, isAsc, SysLoginLog::getBrowser);
                case "os" -> wrapper.orderBy(true, isAsc, SysLoginLog::getOs);
                case "status" -> wrapper.orderBy(true, isAsc, SysLoginLog::getStatus);
                case "loginTime" -> wrapper.orderBy(true, isAsc, SysLoginLog::getLoginTime);
                default -> wrapper.orderByDesc(SysLoginLog::getLoginTime);
            }
        } else {
            // 默认按登录时间倒序
            wrapper.orderByDesc(SysLoginLog::getLoginTime);
        }

        return this.page(new Page<>(query.getCurrent(), query.getSize()), wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void recordLoginLog(String username, String ip, String location, String browser, String os, Integer status, String message) {
        SysLoginLog loginLog = new SysLoginLog();
        loginLog.setUsername(username);
        loginLog.setIp(ip);
        loginLog.setBrowser(browser);
        loginLog.setOs(os);
        loginLog.setStatus(status);
        loginLog.setLoginTime(LocalDateTime.now());

        this.save(loginLog);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void clean() {
        this.remove(new LambdaQueryWrapper<>());
    }
}
